export default {
    namespaced: true,
    state: {
        cartData:[]
    },
    getters: {
        total(state){
            return state.cartData.reduce((total,item)=>{
                return total+=item.price*item.count
            },0)
        }
    },
    mutations: {
        SET_CART(state,payload){
            let target = state.cartData.find(item=>item.id===payload.id)
            if (!target) {
                let obj = {
                    title:payload.title,
                    price:payload.price,
                    id:payload.id,
                    count:1
                }
                state.cartData.push(obj)
            }else{
                target.count++
            }
            
        },
        REMOVE(state,payload){
            payload.count--
            console.log(payload);
            console.log(state.cartData);
            if(payload.count===0){
                state.cartData = state.cartData.filter(item=>item.id!==payload.id)
            }
        }
    },
    actions: {
        ACR(context,payload){
            context.commit('SET_CART',payload)
            // console.log(payload); 
        },
        REMOVE_ACR(context,payload){
            context.commit('REMOVE',payload),
            context.commit('product/PRODUCT_REMOVE',payload,{root:true})
        }
        // PRODUCT_REMOVE2(context,payload){
        //     context.commit('PRODUCT_REMOVE',payload)
        // }
    }
}